﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        bool RandSkip = true;

        //调用声音播放
        SoundPlayer player = new SoundPlayer();

        //计时
        int Timecount = 0;//记录倒计时总毫秒数
        //int isstop = 0;//标示是否启动/暂停的变量，0表示第一次初始化

        //随机选人
        Random randXianShi = new Random();      //随机数种子
        int min = 1;
        int max = 100;
        bool RandomSuspend = true;
        int randXS = 0;
        List<int> RandSkipNum = new List<int> { };

        //随机选题
        Random randtopic = new Random();      //随机数种子
        int topicmin = 1;
        int topicmax = 20;
        bool RandomTopic = true;
        int randTopic = 0;
        List<int> RandSkipSelectedTopic = new List<int> { };

        public Form1()
        {
            InitializeComponent();
            this.MaximizeBox = false;   //禁用“最大化”按钮
            this.FormBorderStyle = FormBorderStyle.FixedDialog;  //设置窗体边框样式为对话框样式
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (RandSkip == false)
            {
                cBxSkipNum.Checked = false;
                lbldomSuspendCount.Visible = false;
            }
            else if (RandSkip == true)
            {
                cBxSkipNum.Checked = true;
                lbldomSuspendCount.Visible = true;
            }

            if (Convert.ToInt32(TxtInitializeMinute.Text) > 0)
            {
                calculateTime(Convert.ToInt32(TxtInitializeMinute.Text));
            }
        }


        private void BtnRandomNum_Click(object sender, EventArgs e)
        {

        }

        private void timerRandomNum_Tick(object sender, EventArgs e)
        {
            try
            {
                do
                {
                    randXS = randXianShi.Next(min, (max + 1));    //产生随机数存入randXS变量
                    TxtRandomNum.Text = randXS.ToString();  //将每次产生的随机数在前台显示
                } while (RandSkip == true && RandSkipNum.Contains(randXS));
            }
            catch { }

        }

        private void BtnRandomNum_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (RandomSuspend == false)
                {

                    if (RandSkip == true && TxtRandomNum.Text != "0")
                    {
                        RandSkipNum.Add(Convert.ToInt32(TxtRandomNum.Text));

                        //将已选题的座位号显示出来
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < RandSkipNum.Count; i++)
                        {
                            if ((i + 1) < RandSkipNum.Count)
                            {
                                sb.Append(RandSkipNum[i] + ",");
                            }
                            if ((i + 1) == RandSkipNum.Count)
                            {
                                sb.Append(RandSkipNum[i]);
                            }
                        }
                        lbllistzwh.Text = sb.ToString();
                        lbldomSuspendCount.Visible = true;
                        lbldomSuspendCount.Text = "还剩 " + Convert.ToString(max - RandSkipNum.Count) + " 个";

                    }
                    if (TxtRandomNum.Text != "0")
                    {
                        TxtRandomNum.ForeColor = Color.Red;
                    }

                    timerRandomNum.Enabled = false;  //启动计时控件开始产生随机数
                    RandomSuspend = true;         //将标记设置为真
                    BtnRandomNum.Text = "开始";      //将按钮的txt值改变
                }
                else if (RandomSuspend == true)
                {
                    if (RandSkipNum.Count == max && RandSkip == true) //判断是否跳过已选并且已选随机数是否已经是最大值
                    {
                        BtnRandomNum.Enabled = false;
                        MessageBox.Show("所有的随机数已经用完！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        timerRandomNum.Enabled = true;  //暂停计时控件产生随机数
                        RandomSuspend = false;         //将标记设置为假
                        TxtRandomNum.ForeColor = Color.Black;
                        BtnRandomNum.Text = "暂停";      //将按钮的txt值改变
                    }
                }
                else
                {
                    MessageBox.Show("出现未知错误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            catch { }
        }

        private void BtnSetting_Click(object sender, EventArgs e)
        {
            try
            {
                //当最小值小于零，最大值大于最小值时，执行赋值
                AllSetting();

                RandSkipNum.Clear();    //清空泛型数组（已选随机数）

            }
            catch { }
        }

        private void AllSetting()
        {
            if (Convert.ToInt32(txtInitializeMin.Text) >= 0 && Convert.ToInt32(txtInitializeMax.Text) > Convert.ToInt32(txtInitializeMin.Text))
            {
                min = Convert.ToInt32(txtInitializeMin.Text);
                max = Convert.ToInt32(txtInitializeMax.Text);

                topicmax = Convert.ToInt32(txtxuanti.Text);

                if (cBxSkipNum.Checked != RandSkip)
                {
                    RandSkip = cBxSkipNum.Checked;  //是否跳过已选过的人

                }
                if (Convert.ToInt32(TxtInitializeMinute.Text) > 0)
                {
                    calculateTime(Convert.ToInt32(TxtInitializeMinute.Text));
                }
            }
            //如果最小值小于0或者最大值小于0发生错误
            else if (Convert.ToInt32(txtInitializeMin.Text) < 0 || Convert.ToInt32(txtInitializeMax.Text) < 0)
            {
                MessageBox.Show("随机数不能小于0！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            //如果最大值小于最小值小于发生错误
            else if (Convert.ToInt32(txtInitializeMax.Text) < Convert.ToInt32(txtInitializeMin.Text))
            {
                MessageBox.Show("随机最大值不能小于最小值！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            //发生未知错误
            else
            {
                MessageBox.Show("出现未知错误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void calculateTime(int minute)
        {
            Timecount = minute * 600;  //毫秒
            lblCountDownHour.Text = (Timecount / 3600000).ToString();
            lblCountDownMinute.Text = ((Timecount / 60000) % 60).ToString();
            lblCountDownSecond.Text = ((Timecount / 1000) % 60).ToString();
            lblCountDownMS.Text = (Timecount % 1000).ToString();
        }

        private void BtnCountDown_Click(object sender, EventArgs e)
        {
            try
            {
                if (BtnCountDown.Text == "开始计时" && lblCountDownHour.Text == "0" && lblCountDownMinute.Text == "0" && lblCountDownSecond.Text == "0" && lblCountDownMS.Text == "0")
                {
                    //当计时器都为0，按钮为开始计时时，重置计时器并开始计时
                    calculateTime(Convert.ToInt32(TxtInitializeMinute.Text));
                    lblCountDownHour.ForeColor = Color.Black;
                    lblCountDownMinute.ForeColor = Color.Black;
                    lblCountDownSecond.ForeColor = Color.Black;
                    lblCountDownMS.ForeColor = Color.Black;
                }
                if (BtnCountDown.Text == "开始计时" || BtnCountDown.Text == "继续计时")
                {
                    //暂停计时
                    this.BtnCountDown.Text = "暂停计时";
                    this.timerCountDown.Interval = 1;
                    this.timerCountDown.Start();
                }
                else
                {
                    this.timerCountDown.Stop();
                    this.BtnCountDown.Text = "继续计时";
                }
            }
            catch { }
        }

        private void timerCountDown_Tick(object sender, EventArgs e)
        {
            try
            {
                if (Timecount >= 0)
                {
                    lblCountDownHour.Text = (Timecount / 3600000).ToString();
                    lblCountDownMinute.Text = ((Timecount / 60000) % 60).ToString();
                    lblCountDownSecond.Text = ((Timecount / 1000) % 60).ToString();
                    lblCountDownMS.Text = (Timecount % 1000).ToString();
                    if (Timecount == 0)
                    {
                        lblCountDownHour.ForeColor = Color.Red;
                        lblCountDownMinute.ForeColor = Color.Red;
                        lblCountDownSecond.ForeColor = Color.Red;
                        lblCountDownMS.ForeColor = Color.Red;
                        timerCountDown.Stop();
                        BtnCountDown.Text = "开始计时";
                    }
                    if (Convert.ToInt32(lblCountDownSecond.Text) <= 5 && Convert.ToInt32(lblCountDownSecond.Text) >= 0 && Convert.ToInt32(lblCountDownMS.Text) == 0 && Convert.ToInt32(lblCountDownHour) == 0 && Convert.ToInt32(lblCountDownMinute) == 0)
                    {
                        switch (lblCountDownSecond.Text)
                        {
                            case "5":
                            case "4":
                            case "3":
                            case "2":
                            case "1":
                                player.SoundLocation = @"C:\Windows\Media\Windows Ding.wav";
                                player.Load();
                                player.Play();
                                break;
                            case "0":
                                player.SoundLocation = @"C:\Windows\Media\Windows Logon Sound.wav";
                                player.Load();
                                player.Play();
                                break;
                            default:
                                break;
                        }

                    }
                    Timecount -= 10;
                }
            }
            catch { }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("您确定要重置程序吗？重置将会清空已选记录。", "重置提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)==DialogResult.OK)
                {
                    AllSetting();

                    ResetInterface();

                    //清空list记录
                    RandSkipSelectedTopic.Clear();
                    RandSkipNum.Clear();
                }

            }
            catch { }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (RandomTopic == false)
                {

                    if (lblSelectedTopic.Text != "0")
                    {
                        RandSkipSelectedTopic.Add(Convert.ToInt32(lblSelectedTopic.Text));
                        lblSelectedTopic.ForeColor = Color.Red;

                        //将已选题的号码显示出来
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < RandSkipSelectedTopic.Count; i++)
                        {
                            if ((i+1) < RandSkipSelectedTopic.Count)
                            {
                                sb.Append(RandSkipSelectedTopic[i] + ",");
                            }
                            else if ((i + 1) == RandSkipSelectedTopic.Count)
                            {
                                sb.Append(RandSkipSelectedTopic[i]);
                            }
                        }
                        lbllisttihao.Text = sb.ToString();
                        lblTopicCount.Text = "还剩 " + Convert.ToString(topicmax - RandSkipSelectedTopic.Count) + " 题";
                    }

                    timerSelectedTopic.Stop();  //启动计时控件开始产生随机数
                    this.timerCountDown.Interval = 1;
                    RandomTopic = true;         //将标记设置为真
                    btnSelectedTopic.Text = "选题";      //将按钮的txt值改变
                }
                else if (RandomTopic == true)
                {
                    if (RandSkipSelectedTopic.Count == topicmax) //判断是否跳过已选并且已选随机数是否已经是最大值
                    {
                        btnSelectedTopic.Enabled = false;
                        MessageBox.Show("所有的题目都已经选完！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        timerSelectedTopic.Start();  //暂停计时控件产生随机数
                        RandomTopic = false;         //将标记设置为假
                        lblSelectedTopic.ForeColor = Color.Black;
                        btnSelectedTopic.Text = "停止";      //将按钮的txt值改变
                    }
                }
                else
                {
                    MessageBox.Show("出现未知错误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            catch { }

        }

        private void timerSelectedTopic_Tick(object sender, EventArgs e)
        {
            try
            {
                do
                {
                    randTopic = randtopic.Next(topicmin, (topicmax + 1));    //产生随机数存入randXS变量
                    lblSelectedTopic.Text = randTopic.ToString();  //将每次产生的随机数在前台显示
                } while (RandSkipSelectedTopic.Contains(randTopic));
            }
            catch { }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定清空记录吗？", "记录清空提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                RandSkipNum.Clear();
                RandSkipSelectedTopic.Clear();
                lbllisttihao.Text = "";
                lbllistzwh.Text = "";
                btnSelectedTopic.Enabled = true;
                BtnRandomNum.Enabled = true;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                AllSetting();

                ResetInterface();
            }
            catch { }
        }

        private void ResetInterface()
        {
            if (RandSkip == false)
            {
                cBxSkipNum.Checked = false;
            }
            else if (RandSkip == true)
            {
                cBxSkipNum.Checked = true;
            }

            if (Convert.ToInt32(TxtInitializeMinute.Text) > 0)
            {
                calculateTime(Convert.ToInt32(TxtInitializeMinute.Text));
            }

            TxtRandomNum.ForeColor = Color.Black;
            lblSelectedTopic.ForeColor = Color.Black;
            lblCountDownHour.ForeColor = Color.Black;
            lblCountDownMinute.ForeColor = Color.Black;
            lblCountDownSecond.ForeColor = Color.Black;
            lblCountDownMS.ForeColor = Color.Black;

            timerCountDown.Enabled = false;
            timerRandomNum.Enabled = false;

            lbllisttihao.Text = "";
            lbllistzwh.Text = "";
            lbldomSuspendCount.Text = "";
            lblTopicCount.Text = "";

            BtnCountDown.Text = "开始计时";
            BtnRandomNum.Text = "开始";
            TxtRandomNum.Text = "0";
            lblSelectedTopic.Text = "0";

            //重置设置文本
            txtxuanti.Text = topicmax.ToString();
            txtInitializeMin.Text = min.ToString();
            txtInitializeMax.Text = max.ToString();
            TxtInitializeMinute.Text = (Timecount / 60000).ToString();
        }

        private void lblCountDownSecond_ClientSizeChanged(object sender, EventArgs e)
        {

        }

    }
}
